//
//  XHPaySuccessViewController.m
//  umibus
//
//  Created by 冷山 on 16/6/18.
//  Copyright © 2016年 lexingsoft. All rights reserved.
//

#import "XHPaySuccessViewController.h"
#import "XHTabBarController.h"

#import "Services.h"
#import "AppDelegate.h"


@interface XHPaySuccessViewController ()

@property (weak, nonatomic) IBOutlet UIButton *ui_okButton;
@property (weak, nonatomic) IBOutlet UILabel *ui_detailLabel;
@property (weak, nonatomic) IBOutlet UITextView *ui_textView;
///购买成功之后赠送积分规则
@property (strong, nonatomic) NSArray *m_buyGifts;
@end

@implementation XHPaySuccessViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"支付结果";
    self.ui_okButton.layer.masksToBounds = YES;
    self.ui_okButton.layer.cornerRadius = 8;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self loadBuyGiftsData];
    [self updateView];
}

- (void)updateView{
    if ([self.productType isEqualToString:kProductTypeCall]) {
        //话费直充
        self.ui_textView.text=@"您的订单已提交成功，您可以选择对应运营商查询入口确认到账情况。如超过24小时仍未查询到账记录，请联系客服，我们将及时为您核实处理。\n温馨提示：月初月末为充值高峰期，由于各运营商定期维护，充值到账可能存在24小时-48小时延迟。";
    }else if ([self.productType isEqualToString:kProductTypeIntelligent]) {
        //智能缴费
        self.ui_textView.text=@"折扣话费套餐中的当月话费立即充值，但由于运营商维护，充值到账可能存在24-48小时的延迟。如果多次查询后仍未到账，请联系客服，我们将及时为您合适处理。\n\n其余月份的话费，将按月于固定每月结算日向您的手机号进行充值操作。";
    }else if ([self.productType isEqualToString:kProductTypeFlow]) {
        //流量直充
        self.ui_textView.text=@"您的订单已提交成功，一般1-30分钟内到账，具体生效时间以运营商短信通知为准。如超过24小时仍未查到充流量到账，请联系客服，我们将及时为您核实处理。\n温馨提示：月初月末为充值高峰期，由于各运营商定期维护，充值到账可能存在24小时-48小时延迟。";
    }else if ([self.productType isEqualToString:kProductTypeFlowPackage]) {
        //流量屯包
        self.ui_textView.text=@"您的订单已提交成功。请移步至个人中心流量屯包处进行充值。";
    }else{
        ///理财流量
        self.ui_textView.text=@"理财流量订单的金额将会为您产生额外的收益，直到回购或者全部使用完毕。额外的收益将于每月的固定结息日进行计算返还，请您在[我的理财收益]或者[订单收益]页面查看对应的收益明细。";
    }
    if (self.m_buyGifts) {
        long price=(long)self.amount;
        NSDictionary *selectedDic=nil;
        for (NSDictionary *dic in self.m_buyGifts) {
            if (price<=[dic[@"tradeUpperLimit"] longValue]) {
                selectedDic=dic;
                break;
            }
        }
        if (!selectedDic) {
            selectedDic=self.m_buyGifts.lastObject;
        }
        long perYunToScore=[selectedDic[@"perYuanToScore"] longValue];
        long score=price*perYunToScore;
        self.ui_detailLabel.text=[NSString stringWithFormat:@"本次购买赠送积分%ld分",score];
    }
}

- (void)loadBuyGiftsData{
    [ServiceManager getBuyGiftsScoreSuccess:^(NSDictionary *data) {
        if ([data isKindOfClass:[NSArray class]]) {
            self.m_buyGifts=(NSArray *)data;
            [self updateView];
        }
    } failure:^(NSDictionary *data) {
        
    }];
}

- (IBAction)paySuccessAction:(id)sender
{
    AppDelegate * delegate = [UIApplication sharedApplication].delegate;
    [delegate.tabBarController goPayFinishOrderView];
    [self.navigationController popToRootViewControllerAnimated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
